package com.heo.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

public class TimeHelper
{
  private static Logger log = Logger.getLogger(TimeHelper.class);

  public static String getMyPath()
  {
    String path = TimeHelper.class.getProtectionDomain().getCodeSource().getLocation().getFile();
    return path;
  }

  /**
   * 获得当前时间的秒数
   * @return
   */
  public static int getCurrSecond()
  {
    return Integer.valueOf(String.valueOf(new Date().getTime() / 1000));
  }

  // private static boolean check()
  // {
  // // 数字保护
  // String endTime = "2017-06-06";
  // int days = daysBetween(new Date(), strToDate(endTime));
  // if (days <= 0)
  // {
  // return false;
  // }
  // return true;
  // }

  /**
   * 增加或减少时间天数
   * 
   * @param date
   * @param num
   * @return
   */
  public static Date addDay(Date date, int num)
  {
    Calendar startDT = Calendar.getInstance();
    startDT.setTime(date);
    startDT.add(Calendar.DAY_OF_MONTH, num);
    return startDT.getTime();
  }

  public static String dateTimeToDateStr(Date datetime)
  {
    try
    {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      return sdf.format(datetime);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      log.error(e);
      return null;
    }
  }

  public static String dateTimeToMonthAndDayStr(Date datetime)
  {
    try
    {
      SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
      return sdf.format(datetime);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      log.error(e);
      return null;
    }
  }

  public static Date strToDateTime(String datetime)
  {
    try
    {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      return sdf.parse(datetime);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      log.error(e);
      return null;
    }
  }

  /**
   * 将时间以指定字符串的形式返回
   * 
   * @param dateTime
   * @param pattern
   * @return
   */
  public static String dateTimeToStr(Date dateTime, String pattern)
  {
    try
    {
      if (dateTime == null)
      {
        return null;
      }
      SimpleDateFormat sdf = new SimpleDateFormat(pattern);
      return sdf.format(dateTime);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      log.error(e);
      return null;
    }
  }

  /**
   * 将时间以"yyyy-MM-dd HH:mm:ss"的形式返回
   * 
   * @param dateTime
   * @return
   */
  public static String dateTimeToStr(Date dateTime)
  {
    return dateTimeToStr(dateTime, "yyyy-MM-dd HH:mm:ss");
  }

  public static Date strToDate(String date)
  {
    try
    {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      return sdf.parse(date);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      log.error(e);
      return null;
    }
  }

  /**
   * 计算两个日期之间相差的天数
   * 
   * @param smdate
   *          较小的时间
   * @param bdate
   *          较大的时间
   * @return 相差天数
   * @throws ParseException
   */
  public static Integer daysBetween(Date smdate, Date bdate)
  {
    try
    {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      smdate = sdf.parse(sdf.format(smdate));
      bdate = sdf.parse(sdf.format(bdate));
      Calendar cal = Calendar.getInstance();
      cal.setTime(smdate);
      long time1 = cal.getTimeInMillis();
      cal.setTime(bdate);
      long time2 = cal.getTimeInMillis();
      long between_days = (time2 - time1) / (1000 * 3600 * 24);
      return Integer.parseInt(String.valueOf(between_days));
    }
    catch (Exception e)
    {
      e.printStackTrace();
      return null;
    }
  }

  /**
   * 判断当前时间是否在时间片段内。时间片段例子为0800，2300
   * 
   * @param nowTime
   * @param startTimeStr
   * @param endTimeStr
   * @return
   */
  public static boolean isInTime(Date nowTime, String startTimeStr, String endTimeStr)
  {
    try
    {
      // 先构造出开始和结束时间
      DateFormat df = DateFormat.getDateInstance();
      String nowDate = df.format(nowTime);
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Date startTime = sdf.parse(nowDate + " " + startTimeStr.substring(0, 2) + ":" + startTimeStr.substring(2, 4) + ":00");
      Date endTime = sdf.parse(nowDate + " " + endTimeStr.substring(0, 2) + ":" + endTimeStr.substring(2, 4) + ":00");
      // 判断关机时间在开机时间的后面
      if (endTime.getTime() - startTime.getTime() > 0)
      {
        // 再判断是否在时间范围内
        if (nowTime.getTime() >= startTime.getTime() && nowTime.getTime() <= endTime.getTime())
        {
          return true;
        }
        else
        {
          return false;
        }
      }
      else
      {
        // 再判断是否在时间范围内
        if (nowTime.getTime() < startTime.getTime() && nowTime.getTime() > endTime.getTime())
        {
          return false;
        }
        else
        {
          return true;
        }
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
      log.error(e);
      return true;
    }
  }
}
